﻿using System;

namespace eProject.Model
{
    /// <summary>
    /// This class represents a time period.
    /// </summary>
    public class Period
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="begin">Begin of time period</param>
        /// <param name="end">End of time period</param>
        /// <exception cref="ArgumentException">If the end is early than the begin </exception>
        public Period(DateTime begin, DateTime end)
        {           
            if (begin >= end)
            {
                throw new ArgumentException("Begin must be early than End");
            }         
            Begin = begin;
            End = end;
            Size = (long) end.Subtract(begin).TotalHours;//TODO think about it
        }

        /// <summary>
        /// The begin of time period
        /// </summary>
        public DateTime Begin { get; private set; }

        /// <summary>
        /// The end of time period
        /// </summary>
        public DateTime End { get; private set; }

        /// <summary>
        /// Time beetwen end and begin (hours).
        /// </summary>
        public long Size { get; private set; }

        /// <summary>
        /// Compares two instance of Period class. Objects is equal when their fields are equal.
        /// </summary>
        /// <param name="left">The first object</param>
        /// <param name="right">The second object</param>
        /// <returns>True if objects are equal</returns>
        public static bool operator ==(Period left, Period right)
        {
            if (ReferenceEquals(null, left)) return false;
            if (ReferenceEquals(null, right)) return false;

            return left.Begin == right.Begin && left.End == right.End;
        }

        public static bool operator !=(Period left, Period right)
        {
            return !(left == right);
        }


        public bool Equals(Period other)
        {
            return this == other;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (Period)) return false;
            return Equals((Period) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (Begin.GetHashCode()*397) ^ End.GetHashCode();
            }
        }
    }
}